package com.stephen.jsbridge.xwalkbridge;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.webkit.ValueCallback;
import android.widget.Button;
import android.widget.LinearLayout;


import com.stephen.jsbridge.jsbridge.OnWebViewListener;
import com.stephen.jsbridge.utils.Config;
import com.stephen.jsbridge.utils.Utils;

import org.xwalk.core.XWalkJavascriptResult;
import org.xwalk.core.XWalkUIClient;
import org.xwalk.core.XWalkView;

import java.io.File;

/**
 * Created by stephen on 10/24/17.
 */

public class XWalkWebViewClient extends XWalkUIClient{
    private Activity activity;
    private XWalkWebView webView;
    private OnWebViewListener onWebViewListener;
    private ValueCallback<Uri> filePathsCallbacks;
    private String systemCameraPicPath = "/sdcard/tempCameraPic.jpg";

    public XWalkWebViewClient(Activity activity, XWalkWebView view, OnWebViewListener onWebViewListener) {
        super(view);
        this.webView = view;
        this.activity = activity;
        this.onWebViewListener = onWebViewListener;
        systemCameraPicPath = Utils.getSD_CardRootPath()+ File.separator+ Config.Project_Dir+ File.separator+"/tempCameraPic.jpg";
    }

    @Override
    public void onReceivedIcon(XWalkView view, String url, Bitmap icon) {
        super.onReceivedIcon(view, url, icon);
    }

    @Override
    public void openFileChooser(XWalkView view, ValueCallback<Uri> uploadFile, String acceptType, String capture) {
        super.openFileChooser(view, uploadFile, acceptType, capture);
        this.filePathsCallbacks = uploadFile;
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        Utils.ViewAndDeleteDirOrFile(systemCameraPicPath);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(systemCameraPicPath)));
        activity.startActivityForResult(intent, XWalkWebView.req_SystemCamera);
    }

    @Override
    public boolean onConsoleMessage(XWalkView view, String message, int lineNumber, String sourceId, ConsoleMessageType messageType) {
        //System.out.println("==Console Log==>message:"+message+"==>messageType:"+messageType+"==>sourceId:"+sourceId+"==>lineNumber:"+lineNumber);
        return super.onConsoleMessage(view, message, lineNumber, sourceId, messageType);
    }

    @Override
    public void onReceivedTitle(XWalkView view, String title) {
        super.onReceivedTitle(view, title);
        if(null != onWebViewListener)onWebViewListener.onReceivedTitle(view,title);
    }

    @Override
    public void onPageLoadStarted(XWalkView view, String url) {
        super.onPageLoadStarted(view, url);
        if(null != onWebViewListener)onWebViewListener.onPageStarted(view,url);
    }

    @Override
    public void onPageLoadStopped(XWalkView view, String url, LoadStatus status) {
        super.onPageLoadStopped(view, url, status);
        if(null != XWalkWebView.toLoadJs)BridgeUtil.webViewLoadLocalJs(view, XWalkWebView.toLoadJs);
        if(webView.getStartupMessage() != null){
            for(Message m : webView.getStartupMessage())webView.dispatchMessage(m);
            webView.setStartupMessage(null);
        }//end of if
        if(null != onWebViewListener)onWebViewListener.onPageFinished(view,url);
    }

    @Override
    public boolean onJsAlert(XWalkView view, String url, String message, XWalkJavascriptResult result) {
        result.cancel();
        return true;
    }

    @Override
    public boolean onJsConfirm(XWalkView view, String url, String message, XWalkJavascriptResult result) {
        result.cancel();
        return true;
    }

    @Override
    public boolean onJsPrompt(XWalkView view, String url, String message, String defaultValue, XWalkJavascriptResult result) {
        result.cancel();
        return true;
    }

    public void callOnActivityResult(int requestCode, int resultCode, Intent intent){
        switch(requestCode){
            case XWalkWebView.req_SystemCamera:
                if(!TextUtils.isEmpty(systemCameraPicPath) && Utils.isFileExits(systemCameraPicPath)){
                    if(null != filePathsCallbacks)filePathsCallbacks.onReceiveValue(Uri.fromFile(new File(systemCameraPicPath)));
                }else{
                    if(null != filePathsCallbacks)filePathsCallbacks.onReceiveValue(null);
                }
                break;
            default:
                if(null != filePathsCallbacks)filePathsCallbacks.onReceiveValue(null);
                break;
        }//end of switch
    }
}
